package com.gomtel.smartdevice.widget;

import android.bluetooth.BluetoothGatt;
import android.content.Context;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleGattCallback;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.gomtel.smartdevice.api.MessageManager;
import com.gomtel.smartdevice.api.bean.ProfileBean;
import com.gomtel.smartdevice.bean.UserContent;
import com.gomtel.smartdevice.global.DataClass;
import com.gomtel.smartdevice.interfaces.MacUtils;
import com.gomtel.smartdevice.moudle.DataManager;
import com.gomtel.smartdevice.moudle.db.entity.SignsOfDataInfo;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.moudle.rxtools.EventBusUtil;
import com.gomtel.smartdevice.ui.activity.HomeActivity;
import com.gomtel.smartdevice.utils.LogUtil;
import com.gomtel.smartdevice.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/28 0028.
 */

public class BlueToothClass {

    public String TAG = "BlueToothClass";
    public ArrayList<Object> objects = new ArrayList<>();
    public String content = "";
    public ToastUtil toastUtil;
    public Context context;
    public DataManager dataManager;

    public BlueToothClass() {

    }

    public BlueToothClass(ToastUtil toastUtil, Context context, DataManager dataManager) {
        this.toastUtil = toastUtil;
        this.context = context;
        this.dataManager = dataManager;
    }

    public void startScan() {
        BleManager instance = BleManager.getInstance();
        if (!instance.isBlueEnable())
            instance.enableBluetooth();
        instance.scan(new BleScanCallback() {
            @Override
            public void onScanStarted(boolean success) {
                LogUtil.e(TAG, "onScanStarted : " + success);
            }

            @Override
            public void onLeScan(BleDevice bleDevice) {
                super.onLeScan(bleDevice);
                if (!content.contains("" + bleDevice.getName())) {
                    content = content + bleDevice.getName();
                    EventBusUtil.sendEvent(new CommonEvent(EventCode.SENDDEVICECONNECTIONCONTENT, bleDevice));
                }
            }

            @Override
            public void onScanning(BleDevice bleDevice) {
                LogUtil.e(TAG, "BleDevice : " + bleDevice);
            }

            @Override
            public void onScanFinished(List<BleDevice> scanResultList) {
                content = "";
                LogUtil.e(TAG, "scanResultList : " + scanResultList);
            }
        });
    }

    //连接
    public void connect(final BleDevice bleDevice) {
        com.clj.fastble.BleManager.getInstance().connect(bleDevice, new BleGattCallback() {
            @Override
            public void onStartConnect() {
                LogUtil.e(TAG, "onStartConnect");
            }

            @Override
            public void onConnectFail(BleDevice bleDevice, BleException exception) {
                LogUtil.e(TAG, "onConnectFail : " + exception);
            }

            @Override
            public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
                toastUtil.showToast("成功连接");
                LogUtil.e(TAG, "onConnectSuccess : " + status);
            }

            @Override
            public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, BluetoothGatt gatt, int status) {
                toastUtil.showToast("断开连接");
                LogUtil.e(TAG, "onDisConnected : " + status);
                EventBusUtil.sendEvent(new CommonEvent(EventCode.CTIONCONTENTSTATUS, status));
            }
        });
    }

    //设置个人信息
    public void SettingSignsOfDataInfo() {
        int sex = 2;
        SignsOfDataInfo signsOfDataInfo = dataManager.querySignsOfDataInfo(UserContent.UserName);
        if (signsOfDataInfo != null) {
            int Age = CalenderUtilsClass.getAgeByBirthDay(signsOfDataInfo.getBirthday());
            if (signsOfDataInfo.getSex().equals("男")) {
                sex = 1;
            }
            ProfileBean profileBean = new ProfileBean();
            profileBean.setAge(Age);
            profileBean.setGender(sex);
            profileBean.setHeight(signsOfDataInfo.getHeight());
            profileBean.setWeight(signsOfDataInfo.getWeight());
            profileBean.setArmLength(signsOfDataInfo.getBrachium());
            LogUtil.e(TAG, "SettingSignsOfDataInfo : " + "Age : " + Age + " - " + "sex : " + sex + " - " +
                    "Height : " + signsOfDataInfo.getHeight() + " - " + "Weight : " + signsOfDataInfo.getWeight() + " - " + "Brachium : " + signsOfDataInfo.getBrachium());
            MessageManager.getInstance().sendPersonInfo(profileBean);
        }
    }

    //绑定设备
    public void BindDevice() {
        MessageManager.getInstance().sendBind(MacUtils.getMac(context));
        LogUtil.e("BlueToothClass","BindDevice");
    }

    //解绑设备
    public void UnBindDevice() {
        HomeActivity.isUnbind = true;
        MessageManager.getInstance().sendUnbind(MacUtils.getMac(context));
    }

    //验证设备
    public void VerificationDevice() {
        MessageManager.getInstance().sendValidation(MacUtils.getMac(context));
        LogUtil.e("BlueToothClass","VerificationDevice");
    }

    //同步时间
    public void SynctimeDevice() {
        MessageManager.getInstance().sendTime();
    }

    //获取系统信息
    public void GetDeviceInfo() {
        MessageManager.getInstance().sendSystemInfo();
    }

    //获取历史步数
    public void GetHistoryStep() {
        MessageManager.getInstance().sendHistoryStep();
    }

    //血压测量
    public void BpMeasure() {
        MessageManager.getInstance().sendTriggerMeasurement(MessageManager.MeasurementID_BP, MessageManager.MeasurementID_STATUS_START);
    }

    //获取历史房颤
    public void GetHistoryAF() {
        MessageManager.getInstance().sendHistoryAF();
    }

    //房颤测量
    public void AFMeasure() {
        MessageManager.getInstance().sendTriggerMeasurement(MessageManager.MeasurementID_AF, MessageManager.MeasurementID_STATUS_START);
    }

    //心电测量
    public void HrvMeasure() {
        MessageManager.getInstance().sendTriggerMeasurement(MessageManager.MeasurementID_HRV, MessageManager.MeasurementID_STATUS_START);
    }

    //目标提醒
    public void ExerciseMeasure() {
        MessageManager.getInstance().sendNotification(MessageManager.NOTIFICATION_ID_EXERCISE, MessageManager.NOTIFICATION_TURN_ON);
    }

    //血压校准
    public void BpcStandard() {
        DataClass.isCalibration = true;
        MessageManager.getInstance().sendBpCalibration();
    }

    //血压清除
    public void BpcStandardClear() {
        DataClass.isCalibration = true;
        MessageManager.getInstance().sendBpDifference(0, 0, 0, 0);
    }
}
